package k2_oop.p5_abstractAndInterface.demo05;

import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class TestDemo5 {

    static Random random = new Random();

    /**
     * 表示 供应商，供应者
     * 无参数，但是有返回值
     */
    public static void testSupplier(){

        Supplier<Integer> supplier = () -> random.nextInt(100);

        System.out.println(supplier.get());
        System.out.println(supplier.get());
        System.out.println(supplier.get());
        System.out.println(supplier.get());
        System.out.println(supplier.get());
    }

    /**
     * 表示 消费者
     * 有参数，但是没有返回值
     */
    public static void testConsumer(){

        Consumer<Integer> consumer = (num) -> System.out.println(num*100);

        consumer.accept(100);
        consumer.accept(24);
        consumer.accept(10);
        consumer.accept(47);
        consumer.accept(98);

    }

    /**
     * 表示 函数
     * 有参数 有返回值
     */
    public static void testFunction(){

        Function<Integer,String> function = (num) -> num+"个西瓜";

        System.out.println(function.apply(10));
        System.out.println(function.apply(9));
        System.out.println(function.apply(3));
        System.out.println(function.apply(6));
        System.out.println(function.apply(8));

    }

    /**
     * 表示 断言
     * 有参数，有返回值，但是返回值必须是boolean类型
     */
    public static void testPredicate(){
        Predicate<Integer> predicate = (num)->num%2==0;

        System.out.println(predicate.test(100));
        System.out.println(predicate.test(97));
        System.out.println(predicate.test(35));
        System.out.println(predicate.test(46));
        System.out.println(predicate.test(12));
    }


    public static void testSupplierStudent(){

        Supplier<Student> studentSupplier = Student::new;
        Student student = studentSupplier.get();
        student.setName("tom");
        student.setAge(20);
        System.out.println(student.getName());
        System.out.println(student.getAge());
    }


    public static void testFunctionString(){
        Function<String,Integer> stringIntegerFunction = String::length;
        System.out.println(stringIntegerFunction.apply("hello"));
    }

    public static void main(String[] args) {
//        TestDemo5.testSupplier();
//        TestDemo5.testConsumer();
//        TestDemo5.testFunction();
//        TestDemo5.testPredicate();
//        TestDemo5.testSupplierStudent();
        TestDemo5.testFunctionString();
    }
}
